package com.easylive.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.List;

/**
 * @Author: yudaxian
 * @DateTime: 2024/11/3 15:44
 **/
public class JsonUtil {
    private static final ObjectMapper jsonMapper = new ObjectMapper();

    public static  <T> T convertStrToObj(String str, Class<T> clz) {
        try {
            return jsonMapper.readValue(str, clz);
        } catch (Exception e) {
            throw new UnsupportedOperationException(e);
        }
    }

    public static <T> String convertObjToStr(T t) {
        try {
            return jsonMapper.writeValueAsString(t);
        } catch (Exception e) {
            throw new UnsupportedOperationException(e);
        }
    }

    public static <T> List<T> convertJsonArray2List(String json, Class<T> clz) {
        try {
            return jsonMapper.readValue(json, jsonMapper.getTypeFactory().constructCollectionType(List.class, clz));
        } catch (Exception e) {
            throw new UnsupportedOperationException(e);
        }
    }
}
